//
//  MBGoodsDetailTypeView.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/5/4.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailTypeView.h"

@interface MBGoodsDetailTypeView ()
@property (nonatomic,weak) UIButton* selectButton;
@property (nonatomic,strong) NSMutableArray* typeArray;
@end

@implementation MBGoodsDetailTypeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSArray* titleArray = @[@"商品",@"详情",@"评价"];
        for (int i=0; i<titleArray.count; i++) {
            
            UIButton* typeButton = [[UIButton alloc]initWithFrame:CGRectMake([UIView lf_sizeFromIphone6:80]*i, 0, [UIView lf_sizeFromIphone6:60], [UIView lf_sizeFromIphone6:30])];
            [self.typeArray addObject:typeButton];
            typeButton.tag = i+1;
            [typeButton setTitle:titleArray[i] forState:UIControlStateNormal];
            [typeButton setTitleColor:COLOR_MAIN forState:UIControlStateSelected];
            [typeButton setTitleColor:[UIColor colorWithR:51 G:51 B:51 A:1] forState:UIControlStateNormal];
            typeButton.titleLabel.font = [UIFont lf_systemFontOfSize:16];
            [typeButton addTarget:self action:@selector(handleClickTypeButton:) forControlEvents:UIControlEventTouchUpInside];
            [typeButton setBackgroundImage:[UIImage imageFromColor:[UIColor colorWithR:51 G:51 B:51] forSize:CGSizeMake([UIView lf_sizeFromIphone6:60], [UIView lf_sizeFromIphone6:27]) withCornerRadius:[UIView lf_sizeFromIphone6:4]] forState:UIControlStateSelected];
            [self addSubview:typeButton];
            
            if (i == 0) {
                typeButton.selected = YES;
                self.selectButton = typeButton;
            }
        }
    }
    return self;
}

-(NSMutableArray *)typeArray
{
    if (_typeArray == nil) {
        _typeArray = [NSMutableArray array];
    }
    return _typeArray;
}

-(void)handleClickTypeButton:(UIButton*)button
{
    if (self.selectButton) {
        self.selectButton.selected = NO;
    }
    button.selected  = YES;
    self.selectButton = button;
    
    self.chooseType = (int)button.tag;
}

-(void)setType:(int)type
{
    _type = type;
    
    UIButton* button = self.typeArray[type];
    
    if (self.selectButton) {
        self.selectButton.selected = NO;
    }
    button.selected  = YES;
    self.selectButton = button;
}


@end
